package com.zkx.fssb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkx.fssb.domain.dto.GiftboxDto;

import com.zkx.fssb.domain.po.Giftbox;
import com.zkx.fssb.exception.BaseException;
import com.zkx.fssb.mapper.GiftboxMapper;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.service.GiftboxService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class GiftboxServiceImpl extends ServiceImpl<GiftboxMapper, Giftbox> implements GiftboxService {

    @Override
    public Page customPage(PageQuery pageQuery) {
        Page page = new Page(pageQuery.getPageNo(), pageQuery.getPageSize());
        LambdaQueryWrapper<Giftbox> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageQuery.getGiftboxName()!=null,Giftbox::getGiftboxName,pageQuery.getGiftboxName());
        this.page(page,wrapper);
        return page;



    }

    @Override
    public void customeSave(GiftboxDto giftboxDto) {

        LambdaQueryWrapper<Giftbox> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Giftbox::getGiftboxName,giftboxDto.getGiftboxName());

        //如果有相同名称的鲜花数据，则新建失败
        if(this.baseMapper.selectOne(wrapper) != null){
            throw new BaseException("新建失败，鲜花名称已存在");
        }else{
            Giftbox giftbox = new Giftbox();
            BeanUtils.copyProperties(giftboxDto,giftbox);
            giftbox.setCreateTime(LocalDateTime.now());
            //如果没有相同名称的鲜花数据，则新增数据
            this.baseMapper.insert(giftbox);
        }


    }

    @Override
    public void customUpdateGiftbox(GiftboxDto giftboxDto) {
        if (giftboxDto.getGiftboxId()==null){

            throw new BaseException("更新失败，鲜花id不能为空");
        }

        //查看表中是否有相同名称的鲜花数据
        LambdaQueryWrapper<Giftbox> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Giftbox::getGiftboxName,giftboxDto.getGiftboxName());

        //如果有相同名称的鲜花数据，则更新失败


            Giftbox giftbox = new Giftbox();
            BeanUtils.copyProperties(giftboxDto,giftbox);
            giftbox.setCreateTime(LocalDateTime.now());
            //如果没有相同名称的鲜花数据，则更新数据
            this.baseMapper.updateById(giftbox);



    }
}
